package com.showsu.editor.job;

import com.showsu.editor.entity.doo.User;
import com.showsu.editor.entity.po.TblUserExppackage;
import com.showsu.editor.service.IUserExppackageService;
import com.showsu.editor.service.IUserPrivilegeService;
import com.showsu.editor.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author: William
 * @Description: 每天处理客户套餐到期
 * @Date: Created in 3:51 下午 2024/9/18
 * @Modified By:
 */
@Component
@Slf4j
public class VipEndSchedule {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserPrivilegeService userPrivilegeService;

    @Autowired
    private IUserExppackageService userExppackageService;

    /**
     * 任务启动入口
     */
    @Scheduled(cron = "0 12 8,17 * * ? ")
    //@Scheduled(cron = "0 0 17 * * ? ")
    public void execute() {
        log.info("每天处理客户 套餐到期/资源包到期 的任务，启动");
        //处理到期用户及其权益数据
        List<User> userList = userService.queryExpiredUser();
        if (! CollectionUtils.isEmpty(userList)) {
            for (User user : userList) {
                //处理用户信息
                int result0 = userService.expiredUser(user.getId());
                //处理用户权益信息
                int result1 = userPrivilegeService.exipreUserPrivilege(user.getId());
                log.info("用户:{} 到期了，对该用户做降级处理，处理结果:{},{}", user.getId(), result0, result1);
            }
        }
        //处理到期资源包数据
        List<TblUserExppackage> userExppackageList = userExppackageService.queryExpiredUserExppackage();
        if (! CollectionUtils.isEmpty(userExppackageList)) {
            for (TblUserExppackage userExppackage : userExppackageList) {
                int result = userExppackageService.exipreUserExppackage(userExppackage.getId());
                log.info("用户:{} 的资源包:{} 到期了，对资源包做到期处理，处理结果:{}", userExppackage.getUserId(), userExppackage.getId(), result);
            }

        }
        log.info("每天处理 套餐到期/资源包到期 的任务，结束");
    }
}
